<template>
  <div>
    <el-table ref="tableRef" v-loading="loading" :data="dataSource" :show-header="parentId === '#'" @row-click="menuClick" :height="autoHeight">
      <el-table-column type="expand" align="center" label="下级">
        <menu-table slot-scope="scope" :parent-id="scope.row.menuId" style="padding-left: 12px"/>
      </el-table-column>
      <el-table-column prop="title" label="标题" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" align="center"></el-table-column>
      <el-table-column prop="icon" label="图标" align="center">
        <i slot-scope="scope" :class="scope.row.icon" class="font14"></i>
      </el-table-column>
      <el-table-column prop="component" label="组件" align="center">
        <span slot-scope="scope">{{ components.find(item => item.value === scope.row.component) ? components.find(item => item.value === scope.row.component).label : scope.row.component }}</span>
      </el-table-column>
      <el-table-column prop="menuOrder" label="排序" align="center" width="50px"></el-table-column>
      <el-table-column prop="dataState" label="状态" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState >=0 ">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" width="130px">
        <template slot="header" slot-scope="scope" >
          <el-button v-if="scope && parentId === '#'" type="primary" @click="visible = true" icon="el-icon-circle-plus-outline">新增</el-button>
          <span v-else>操作</span>
        </template>
        <div slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" circle @click="() => { menuInfo=$Util.copy(scope.row);visible=true}" title="编辑"></el-button>
          <el-button type="warning" icon="el-icon-circle-plus-outline" circle @click="() => { menuInfo.parentId=scope.row.menuId;visible=true}" title="添加"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
        </div>
      </el-table-column>
    </el-table>
<!--    <div class="lab-table-page">-->
<!--      <div style="display: inline-flex">-->
<!--        <el-pagination-->
<!--            :disabled="loading"-->
<!--            :current-page="pageInfo.page"-->
<!--            :page-sizes="[5, 10, 15, 20, 50, 100]"-->
<!--            :page-size="pageInfo.pageSize"-->
<!--            layout="total, sizes, prev, pager, next, jumper"-->
<!--            :total="pageInfo.total"-->
<!--            @size-change="handleSizeChange"-->
<!--            @current-change="handleCurrentChange"-->
<!--        />-->
<!--        <el-button-group style="padding: 2px 5px">-->
<!--          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />-->
<!--        </el-button-group>-->
<!--      </div>-->
<!--    </div>-->

    <el-dialog :visible.sync="visible" title="菜单信息">
      <el-form label-position="right" label-width="80px">
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="菜单标题" required>
              <el-input v-model="menuInfo.title" placeholder="展示名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="渲染组件" required>
              <el-select value="" v-model="menuInfo.component" allow-create filterable placeholder="渲染组件" style="width: 100%">
                <el-option v-for="item in components" :key="item.value" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单图标">
              <idea-icon v-model="menuInfo.icon" placeholder="展示图标"></idea-icon>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序">
              <el-input-number v-model="menuInfo.menuOrder" controls-position="right" placeholder="展示顺序" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="菜单路由" required>
          <el-input v-model="menuInfo.path" placeholder="访问路径"/>
        </el-form-item>
        <el-form-item label="转发路由" v-if="menuInfo.component && menuInfo.component.startsWith('layout')">
          <el-input v-model="menuInfo.redirect" placeholder="转发菜单" />
        </el-form-item>
        <el-form-item label="唯一名称">
          <el-input v-model="menuInfo.name" placeholder="唯一标识"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="menuInfo.dataState">
                <el-radio :label="-1">禁用</el-radio>
                <el-radio :label="0">正常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
<!--          <el-col :span="6">
            <el-form-item label="缓存">
              <el-switch v-model="menuInfo.isKeepAlive"/>
            </el-form-item>
          </el-col>-->
          <el-col :span="6">
            <el-form-item label="隐藏">
              <el-switch v-model="menuInfo.isHide"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="固定">
              <el-switch v-model="menuInfo.isAffix"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="外链">
              <el-switch v-model="menuInfo.isLink"/>
            </el-form-item>
          </el-col>
          <el-col :span="6" v-if="menuInfo.isLink">
            <el-form-item label="iframe">
              <el-switch v-model="menuInfo.isIframe"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item v-if="menuInfo.isLink" label="外链地址">
          <el-input v-model="menuInfo.hrefSrc" placeholder="外链地址,http://或者https://开头"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(menuInfo)">{{ menuInfo.menuId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import IdeaIcon from "@/components/idea-icon/IdeaIcon";

export default {
  name: "MenuTable",
  components: {IdeaIcon},
  mixins: [IdeaTable],
  inject: {
    menuClick: {
      from: 'menuClick',
      default: function () {
        return function (){}
      }
    }
  },
  props: {
    parentId: {
      type: String,
      default: '#'
    }
  },
  data () {
    return {
      menuInfo: { isKeepAlive: true },
      components: [
        { value: 'layout/index', label: '菜单布局' },
        { value: 'layout/routerView/parent', label: '空白布局' },
        { value: 'ViewModel', label: '视图模型' }
      ]
    }
  },
  computed: {
    autoHeight(){
      return this.parentId === '#' ? `calc(100vh - 180px)`: ''
    }
  },
  watch: {
    visible(){
      if (!this.visible) {
        this.initInfo()
      }
    }
  },
  created() {
    this.queryUrl = '/menu/queryPage'
    this.saveUrl = '/menu/save'
    this.removeUrl = '/menu/delete/'
    this.fixedFilters.push({ field: 'parentId', op: '=', value: this.parentId })
    this.rowKey = 'menuId'
    this.pageInfo.pageSize = 100
    this.pageInfo.sorts.push({ field: 'menuOrder', asc: true })
    this.loadSource()
    this.initInfo()
  },
  methods: {
    initInfo(){
      this.menuInfo = { isKeepAlive: true, parentId: this.parentId, clientId: process.env.VUE_APP_CLIENT_ID }
    }
  }
}
</script>

<style scoped>

</style>
